package com.ecoolex.ying.space.admin.graphql;


import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.ying.space.admin.core.AdminConstant;
import com.ecoolex.ying.space.entity.MarktingCatalog;
import com.ecoolex.ying.space.pojo.AddMarktingCatalogDTO;
import com.ecoolex.ying.space.pojo.EditMarktingCatalogDTO;
import com.ecoolex.ying.space.pojo.QueryMarktingCatalogDTO;
import com.ecoolex.ying.space.ws.MarktingCatalogGoodsService;
import com.ecoolex.ying.space.ws.MarktingCatalogService;
import com.google.common.collect.Lists;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

@GraphQL2Module(type = GraphQL2Module.Type.Admin, description = "运营配置-目录管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY, key = MarktingCatalogGraphql.AUTH, name = "运营配置目录管理")
public class MarktingCatalogGraphql {

    @Reference
    private MarktingCatalogService marktingCatalogService;
    
    @Reference
    private MarktingCatalogGoodsService marktingCatalogGoodsService;

    public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-MARKETING-CATALOG";
    
    @TypeField(type = MarktingCatalog.class,field = "marktingCatalogGoodsCount",description = "目录关联商品数量")
    public long marktingCatalogGoodsCount(@Source MarktingCatalog source) {
    	return marktingCatalogGoodsService.countCatalogGoodsByCatalogId(source.getCatalogId());
    }
    
    @TypeField(type = MarktingCatalog.class,field = "canOnline",description = "能上线")
    public boolean canOnline(@Source MarktingCatalog source) {
    	return !source.getOnline();
    }
    
    @TypeField(type = MarktingCatalog.class,field = "canOffline",description = "能下线")
    public boolean canOffline(@Source MarktingCatalog source) {
    	return source.getOnline();
    }

    private static final String AUTH_GET = AUTH + "-GET";

    @QueryField(field = "marktingCatalog", description = "获取运营目录")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "获取运营目录详情")
    public MarktingCatalog get(@NonNull Integer catalogId, AdminMemberAuthority user) {
        user.assertLogin();
        return marktingCatalogService.getCatalogById(catalogId);
    }

    private static final String AUTH_QUERY = AUTH + "-QUERY";

    @QueryField(field = "marktingCatalogList", description = "查询运营目录列表")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "查询运营目录列表")
    public Pagination<MarktingCatalog> queryMarktingCatalog(@NonNull QueryMarktingCatalogDTO query, @NonNull Sorting[] sorts, @NonNull Page page, AdminMemberAuthority user) {
        user.assertLogin();
        return marktingCatalogService.queryMarktingCatalog(query, sorts, page);
    }

    private static final String AUTH_ADD = AUTH + "-ADD";

    @MutationField(field = "addMarktingCatalog", description = "添加运营目录")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加运营目录")
    public MarktingCatalog add(@NonNull AddMarktingCatalogDTO add, AdminMemberAuthority user) {
        user.assertAuth(AUTH_ADD);
        return marktingCatalogService.addCatalog(add, user.userName());
    }

    private static final String AUTH_EDIT = AUTH + "-EDIT";

    @MutationField(field = "editMarktingCatalog", description = "编辑运营目录")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑运营目录")
    public MarktingCatalog edit(@NonNull EditMarktingCatalogDTO edit, AdminMemberAuthority user) {
        user.assertAuth(AUTH_EDIT);
        MarktingCatalog old = marktingCatalogService.getCatalogById(edit.getCatalogId());
        Assert.isTrue(Check.notNull(old), ResultCode.VERIFY.build(1), "目录不存在");
        BeanUtil.copy(edit,
                old,
                Lists.newArrayList(
                        MarktingCatalog.Column.deleted,
                        MarktingCatalog.Column.createUser,
                        MarktingCatalog.Column.createTime).stream().map(MarktingCatalog.Column::getJavaProperty).toArray(String[]::new));
        return marktingCatalogService.updateCatalog(old, user.userName());
    }

    private static final String AUTH_DEL = AUTH + "-DEL";

    @MutationField(field = "delMarktingCatalog", description = "删除运营目录")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除运营目录")
    public Boolean del(@NonNull Integer catalogId, AdminMemberAuthority user) {
        user.assertAuth(AUTH_DEL);
        MarktingCatalog record = marktingCatalogService.getCatalogById(catalogId);
        Assert.isTrue(Check.notNull(record), ResultCode.VERIFY.build(1), "目录不存在");
        marktingCatalogService.deleteCatalog(record, user.userName());
        return true;
    }

    private static final String AUTH_ONLINE = AUTH + "-ONLINE";
    
    @MutationField(field = "onlineMarktingCatalog", description = "上线运营目录")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ONLINE, name = "上线运营目录")
    public Boolean online(@NonNull Integer catalogId, AdminMemberAuthority user) {
        user.assertAuth(AUTH_ONLINE);
        MarktingCatalog record = marktingCatalogService.getCatalogById(catalogId);
        Assert.isTrue(Check.notNull(record), ResultCode.VERIFY.build(1), "目录不存在");
        marktingCatalogService.online(record, user.userName());
        return true;
    }
    
    private static final String AUTH_OFFLINE = AUTH + "-OFFLINE";

    @MutationField(field = "offlineMarktingCatalog", description = "下线运营目录")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_OFFLINE, name = "下线运营目录")
    public Boolean offline(@NonNull Integer catalogId, AdminMemberAuthority user) {
        user.assertAuth(AUTH_OFFLINE);
        MarktingCatalog record = marktingCatalogService.getCatalogById(catalogId);
        Assert.isTrue(Check.notNull(record), ResultCode.VERIFY.build(1), "目录不存在");
        marktingCatalogService.offline(record, user.userName());
        return true;
    }
}
